package com.apes.fn.scm.retailOrder.service;

import afu.org.checkerframework.checker.units.qual.A;
import com.apes.fn.scm.retailOrder.model.OverDiscountItem;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.model.RetailOrderItem;
import com.apes.fn.scm.retailOrder.repository.RetailOrderRepository;
import com.apes.fn.scm.retailOrder.repository.OverDiscountItemRepository;
import com.apes.fn.scm.retailReturn.RetailReturn;
import com.apes.fn.scm.retailReturn.RetailReturnItem;
import com.apes.fn.scm.retailReturn.RetailReturnRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.scm.masterdata.product.model.ServeType;
import com.apes.scm.masterdata.product.repository.ServeTypeRepository;
import com.apes.scm.masterdata.product.service.ServeCategoryService;
import com.apes.scm.masterdata.product.service.ServeTypeService;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.repository.VoucherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: apes-server
 * @description
 * @author: czs
 * @create: 2022-01-06 11:46
 **/
@Service("overDiscountItemService")
public class OverDiscountItemService extends DomainService {

    @Autowired
    private OverDiscountItemRepository overDiscountItemRepository;

    @Autowired
    private RetailOrderRepository retailOrderRepository;

    @Autowired
    private RetailReturnRepository retailReturnRepository;

    @Autowired
    private VoucherRepository voucherRepository;

    @OutboxSubscriber(name = "超额抵扣明细", condition = " originBusinessType == 'RetailReturn' || originBusinessType == 'RetailOrder' ")
    public void subscribeVoucher(Voucher voucher) {
        Voucher newVoucher = voucherRepository.findOne(voucher.getId());
        if (!("CUSTOMER_SALE".equals(newVoucher.getVoucherType()) || "CUSTOMER_CONSUME".equals(newVoucher.getVoucherType())))
            return;
        RetailOrder retailOrder = null;
        RetailReturn retailReturn;

        if (newVoucher.getOrigin().startsWith("LT")) {
            retailReturn = retailReturnRepository.findOne(newVoucher.getOrigin());
            retailOrder = retailReturn.getSaleOrder();
        } else {
            retailOrder = retailOrderRepository.findOne(newVoucher.getOrigin());
        }

        if (retailOrder == null) throw new RuntimeException("错误的来源,凭证单号为:" + newVoucher.getId());
        if (retailOrder.getInsuranceDiscounts() != null) return;
        String yyms = retailOrder.getDept().getCompany().getParty().getCorporate().getOperation().getId();
        if (!(yyms.equals("001072") || yyms.equals("001661"))) return;

        List<VoucherItem> voucherItemList = newVoucher.getVoucherItems().stream()
                .filter(item -> item.getProduct().getProductGroup().getId().equals("10"))
                .filter(item -> {
                    double price = item.getPriceTotal() - item.getDiscount() - (item.getProductQty() * item.getTaxCostPrice());
                    if (newVoucher.getOrigin().startsWith("LT")) return price > 1;
                    return price < -1;
                }).collect(Collectors.toList());

        for (VoucherItem voucherItem : voucherItemList) {
            OverDiscountItem overDiscountItem = new OverDiscountItem();
            if (newVoucher.getOrigin().startsWith("LT")) {
                overDiscountItem.setSourceBusinessTypeName("retailReturn");
            } else {
                overDiscountItem.setSourceBusinessTypeName("retailOrder");
            }
            overDiscountItem.setSourceBusinessKey(retailOrder.getId());
            overDiscountItem.setSourceBusinessDate(retailOrder.getCreateDate());
            overDiscountItem.setDept(retailOrder.getDept());
            overDiscountItem.setCompany(retailOrder.getCompany());
            overDiscountItem.setProduct(voucherItem.getProduct());
            overDiscountItem.setSaleQty(voucherItem.getProductQty());
            overDiscountItem.setPriceInTax(voucherItem.getPriceUnit());
            overDiscountItem.setPriceInTaxTotal(voucherItem.getPriceTotal());
            overDiscountItem.setReferTax(voucherItem.getProductQty() * voucherItem.getTaxCostPrice());
            overDiscountItem.setDiscountPrice(voucherItem.getDiscount());
            overDiscountItem.setAmtProfit(voucherItem.getPriceTotal() - voucherItem.getDiscount() - (voucherItem.getProductQty() * voucherItem.getTaxCostPrice()));
            overDiscountItem.setUom(voucherItem.getUom());
            overDiscountItem.setTax(voucherItem.getTax());
            overDiscountItem.setPriceTax(voucherItem.getPriceTax());
            overDiscountItem.setPriceSubtotal(voucherItem.getPriceSubtotal());
            overDiscountItem.setCustomerExpand(retailOrder.getCustomerExpand());
            overDiscountItem.setServeType(SpringManager.getBean(ServeCategoryService.class).findByClassify(voucherItem.getProduct().getClassify()).getServeType());
            overDiscountItemRepository.saveAndFlush(overDiscountItem);
        }
    }
}
